package cn.com.hbhgkj.webapp.taglib;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.servlet.jsp.JspException;

import org.apache.struts2.views.jsp.ui.SelectTag;

import cn.com.hbhgkj.model.LabelValue;

import com.opensymphony.xwork2.ActionContext;

public class GardenTag extends SelectTag {

	public int doStartTag() throws JspException {
		List<LabelValue> targetList = buildIDTypesList(pageContext.getRequest().getLocale());
		ActionContext.getContext().getValueStack()
				.set("crs_gardens", targetList);
		this.setList("%{crs_gardens}");
		return super.doStartTag();
	}

	/**
	 * Build a List of LabelValues for all the available countries. Uses the two
	 * letter uppercase ISO name of the country as the value and the localized
	 * country name as the label.
	 * 
	 * @param locale
	 *            The Locale used to localize the country names.
	 * 
	 * @return List of LabelValues for all available countries.
	 */
	@SuppressWarnings("unchecked")
	private List<LabelValue> buildIDTypesList(Locale locale) {
		List<LabelValue> gardens = new ArrayList<LabelValue>();
		gardens.add(new LabelValue("",""));
		gardens.add(new LabelValue("清华软件园","清华软件园"));
		gardens.add(new LabelValue("上海浦东软件园","上海浦东软件园"));
		gardens.add(new LabelValue("杭州软件园","杭州软件园"));
		gardens.add(new LabelValue("江苏软件园","江苏软件园"));
		gardens.add(new LabelValue("东大软件园","东大软件园"));
		gardens.add(new LabelValue("齐鲁软件园","齐鲁软件园"));
		gardens.add(new LabelValue("西部软件园","西部软件园"));
		gardens.add(new LabelValue("长沙软件园","长沙软件园"));
		gardens.add(new LabelValue("北京软件产业基地","北京软件产业基地"));
		gardens.add(new LabelValue("天津华苑软件园","天津华苑软件园"));
		gardens.add(new LabelValue("湖北软件产业基地","湖北软件产业基地"));
		gardens.add(new LabelValue("杭州高新软件园","杭州高新软件园"));
		gardens.add(new LabelValue("福州软件园","福州软件园"));
		gardens.add(new LabelValue("江西金庐软件园","江西金庐软件园"));
		gardens.add(new LabelValue("西安软件园","西安软件园"));
		gardens.add(new LabelValue("大连软件园","大连软件园"));
		gardens.add(new LabelValue("广州软件园","广州软件园"));
		gardens.add(new LabelValue("上海软件园","上海软件园"));
		gardens.add(new LabelValue("南京软件园","南京软件园"));
		gardens.add(new LabelValue("长春软件园","长春软件园"));
		gardens.add(new LabelValue("厦门软件园","厦门软件园"));
		gardens.add(new LabelValue("合肥软件园","合肥软件园"));
		gardens.add(new LabelValue("云南软件园","云南软件园"));
		gardens.add(new LabelValue("深圳软件园","深圳软件园"));
		gardens.add(new LabelValue("兰州软件园","兰州软件园"));
		gardens.add(new LabelValue("青岛软件园","青岛软件园"));
		gardens.add(new LabelValue("吉林软件园","吉林软件园"));
		gardens.add(new LabelValue("中部软件产业园","中部软件产业园"));
		gardens.add(new LabelValue("无锡软件园","无锡软件园"));
		gardens.add(new LabelValue("苏州软件园","苏州软件园"));
		gardens.add(new LabelValue("常州软件园","常州软件园"));
		gardens.add(new LabelValue("宁波国际软件园","宁波国际软件园"));
		gardens.add(new LabelValue("河北省软件产业基地（石家庄）","河北省软件产业基地（石家庄）"));
		gardens.add(new LabelValue("其他园区","其他园区"));
		Collections.sort(gardens, new LabelValueComparator(locale));
		return gardens;
	}

	/**
	 * Class to compare LabelValues using their labels with locale-sensitive
	 * behaviour.
	 */
	public class LabelValueComparator implements Comparator {
		private Comparator c;

		/**
		 * Creates a new LabelValueComparator object.
		 * 
		 * @param locale
		 *            The Locale used for localized String comparison.
		 */
		public LabelValueComparator(final Locale locale) {
			c = Collator.getInstance(locale);
		}

		/**
		 * Compares the localized labels of two LabelValues.
		 * 
		 * @param o1
		 *            The first LabelValue to compare.
		 * @param o2
		 *            The second LabelValue to compare.
		 * 
		 * @return The value returned by comparing the localized labels.
		 */
		@SuppressWarnings("unchecked")
		public final int compare(Object o1, Object o2) {
			LabelValue lhs = (LabelValue) o1;
			LabelValue rhs = (LabelValue) o2;

			return c.compare(lhs.getLabel(), rhs.getLabel());
		}
	}
}
